import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * 
 * @author Srinidhi Chaganty and Sammy Warrayat
 * 
 * This class is another screen that allows a user to pick both the 
 * name, race and color of their player.
 *
 */
@SuppressWarnings("serial")
public class PlayerGUI extends JPanel
{
	/*
	 * colors is an Arraylist of all the color types (used in selection screen in startup)
	 * playerInfo is an Array. Each element contains all of the Players info (name, race, color)
	 * jt is where the user will type the player's name
	 */
	private ArrayList<Color> colors;
	private Object[] playerInfo;
	private JTextField jt;
	
	/**
	 * The no-args constructor to set up the PlayerGUI
	 * @throws IOException 
	 */
	public PlayerGUI() throws IOException{
		colors = new ArrayList<Color>();
		colors.add(GameModel.ourRed);
		colors.add(GameModel.ourBlue);
		colors.add(GameModel.ourYellow);
		colors.add(GameModel.ourPurple);
		this.setLayout(new BorderLayout());
		//first item is name, second is race, third is color and fourth is a boolean for
		//whether or not the other 3 objects are valid
		playerInfo = new Object[4];
		addPanels();
	}
	/**
	 * A constructor for PlayerGUI that takes in an arrayList that takes in
	 * the colors left for players to choose
	 * 
	 * @param colorsLeft the colors left
	 * @throws IOException 
	 */
	public PlayerGUI(ArrayList<Color>colorsLeft) throws IOException{
		colors = colorsLeft;
		this.setLayout(new BorderLayout());
		//first item is name, second is race, third is color and fourth is a boolean for
		//whether or not the other 3 objects are valid
		playerInfo = new Object[4];
		addPanels();
	}
	/**
	 * returns the playerInfo array
	 * @return Object[] the player info array
	 */
	public Object[] getPlayerInfo() {
		return playerInfo;
	}

	/**
	 * adds all the panels to the main JPanel
	 * @throws IOException 
	 */
	private void addPanels() throws IOException{
		//the intro panel is created here (holds intro JLabel)
		JPanel introPanel = new JPanel();
		//the intro label (added to introPanel)
		JLabel intro = new JLabel("CUSTOMIZE YOUR PLAYER:");
		introPanel.add(intro);
		/*
		 * addPanel1 contains the introPanel and namePanel
		 * addPanel2 contains the racePanel and colorPanel
		 */
		JPanel addPanel1 = new JPanel(new BorderLayout());
		JPanel addPanel2 = new JPanel(new BorderLayout());
		addPanel1.add(introPanel, BorderLayout.PAGE_START);
		addPanel1.add(namePanel(), BorderLayout.CENTER);
		addPanel2.add(racePanel(), BorderLayout.PAGE_START);
		addPanel2.add(colorPanel(), BorderLayout.CENTER);
		this.add(addPanel1, BorderLayout.PAGE_START);
		this.add(addPanel2, BorderLayout.CENTER);
		this.add(endPanel(), BorderLayout.PAGE_END);
	}

	/**
	 * sets up a panel for entering the name of the player
	 * 
	 * @return JPanel the panel for the name
	 */
	private JPanel namePanel(){
		//namePanel is returned by this method
		JPanel namePanel = new JPanel(new BorderLayout());
		//name is a JLabel that prompts the user
		JLabel name = new JLabel("Please enter a name: ");
		jt = new JTextField(20);
		namePanel.add(name, BorderLayout.PAGE_START);
		namePanel.add(jt, BorderLayout.CENTER);
		return namePanel;
	}

	/**
	 * sets up a panel for choosing the race of the player
	 * 
	 * @return JPanel the panel for choosing the race
	 * @throws IOException 
	 */
	private JPanel racePanel() throws IOException{
		//racePanel is returned by this method
		JPanel racePanel = new JPanel();
		//label
		//final JLabel racePic = new JLabel("Heyooooo");
		//race is a JLabel that prompts the user
		JLabel race = new JLabel("Pick a race: ");
		//oneButton is a JButton that represents the Human race
		//final JButton oneButton = new JButton("Human");
		final PicButton oneButton = new PicButton(getClass().getResource("Images/person_hum.png"));
		oneButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == oneButton) {
					playerInfo[1] = "Human";
					//racePic.setIcon(icon);
					}}});
		//twoButton is a JButton that represents the Flapper race
		final PicButton twoButton = new PicButton(getClass().getResource("Images/person_flap.png"));
		twoButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == twoButton)
					playerInfo[1] = "Flapper";}});
		//threeButton is a JButton that represents the Alien race
		final PicButton threeButton = new PicButton(getClass().getResource("Images/person_alien.png"));
		threeButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == threeButton)
					playerInfo[1] = "Alien";}});
		//fourButton is a JButton that represents the Batman race
		final PicButton fourButton = new PicButton(getClass().getResource("Images/person_batman.png"));
		fourButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == fourButton)
					playerInfo[1] = "Batman";}});
		racePanel.add(race);
		racePanel.add(oneButton);
		racePanel.add(twoButton);
		racePanel.add(threeButton);
		racePanel.add(fourButton);
		//racePanel.add(racePic);
		return racePanel;
	}

	/**
	 * sets up a panel for choosing the color of the player
	 * 
	 * @return JPanel the panel for choosing the color
	 */
	private JPanel colorPanel(){
		//colorPanel is returned by this method
		JPanel colorPanel = new JPanel();
		//color is a JLabel that prompts the user
		JLabel color = new JLabel("Pick a color: ");
		//oneButton is a JButton that represents the color Red
		final JButton oneButton = new JButton("Red");
		oneButton.setBackground(GameModel.ourRed);
		oneButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == oneButton)
					playerInfo[0] = GameModel.ourRed;}});
		//twoButton is a JButton that represents the color Yellow
		final JButton twoButton = new JButton("Yellow");
		twoButton.setBackground(GameModel.ourYellow);
		twoButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == twoButton)
					playerInfo[0] = GameModel.ourYellow;}});
		//threeButton is a JButton that represents the color Blue
		final JButton threeButton = new JButton("Blue");
		threeButton.setBackground(GameModel.ourBlue);
		threeButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == threeButton)
					playerInfo[0] = GameModel.ourBlue;}});
		//fourButton is a JButton that represents the color Purple
		final JButton fourButton = new JButton("Purple");
		fourButton.setBackground(GameModel.ourPurple);
		fourButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == fourButton)
					playerInfo[0] = GameModel.ourPurple;}});
		colorPanel.add(color);
		//check if the colors array has each color- if not, the color
		//has already been chosen by another player, so it doesn't get added
		if(colors.contains(GameModel.ourRed))
			colorPanel.add(oneButton);
		if(colors.contains(GameModel.ourYellow))
			colorPanel.add(twoButton);
		if(colors.contains(GameModel.ourBlue))
			colorPanel.add(threeButton);
		if(colors.contains(GameModel.ourPurple))
			colorPanel.add(fourButton);
		return colorPanel;
	}

	/**
	 * sets up a panel for the next button
	 * 
	 * @return JPanel the panel for the next button
	 */
	private JPanel endPanel(){
		//endPanel is returned by this method
		final JPanel endPanel = new JPanel();
		//nextButton is a button that goes to the next screen 
		final JButton nextButton = new JButton("Next: ");
		//next button checks if all the fields have been filled in
		nextButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == nextButton){
					if(jt.getText().length() > 1)
						playerInfo[2] = jt.getText();
					if(playerInfo[0] != null && playerInfo[1] != null && playerInfo[2] != null)
						playerInfo[3] = true;
					else{
						playerInfo[3] = false;
						//error is a JLabel that tells the user that they have not filled all of the fields
						JLabel error = new JLabel("Please fill in all the fields");
						endPanel.revalidate();
						endPanel.add(error);}}}});
		endPanel.add(nextButton);
		return endPanel;
	}
	/**
	 * returns the ArrayList with the colors arrayList
	 * @return ArrayList<Color> the arraylist of colors
	 */
	public ArrayList<Color> getColors() {
		return colors;
	}
	
}